﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;

namespace XBMCRemote.DataModel.Entities
{
    public class Movie
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Plot { get; set; }
        public string Genre { get; set; }
        public IList<string> Genres { get; set; }
        public int Duration { get; set; }
        public float Rating { get; set; }
        public int Year { get; set; }
        public char GroupingCharacter { get { return Title.Substring(0, 1).ToCharArray()[0]; } }
        public string  DisplayTitle {get { return Title + " (" + Year + ")"; }}

        public static Movie FromJObject(JObject movieObject)
        {
            Movie movie = new Movie();
            foreach (var movieProperty in movieObject.Properties())
            {
                switch( movieProperty.Name)
                {
                    case "movieid":
                        movie.Id = (int) movieProperty.Value.Value<JValue>();
                        break;
                    case "title":
                        movie.Title = (string) movieProperty.Value.Value<JValue>();
                        break;
                    case "plot":
                        movie.Plot = (string)movieProperty.Value.Value<JValue>();
                        break;
                    case "genre":
                        string genres =(string)movieProperty.Value.Value<JValue>();
                        string[] genreArray = genres.Split('/');
                        List<string> genreList = genreArray.ToList();
                        genreList.ForEach(g => g.Trim());
                        movie.Genre = genres;
                        movie.Genres = genreList;
                        break;
                    case "runtime":
                        movie.Duration = Int32.Parse((string)movieProperty.Value.Value<JValue>());
                        break;
                    case "rating":
                        movie.Rating = (float)movieProperty.Value.Value<JValue>();
                        break;
                    case "year":
                        movie.Year = (int)movieProperty.Value.Value<JValue>();
                        break;
                }
            }
            return movie;
        }
    }
}
